﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using com.vs.General;
using com.vs.DataAccess;

namespace com.vs.DataAccess.DO
{
    public partial class VendorDO
    {

        #region "Get Model"
        /// <summary>
        /// 将符合数据库主键的持久对象提取到模型对象中，如数据库符合条件的持久对象不存在，则返回空对象
        /// </summary>
        /// <remarks></remarks>
        public static VendorDO GetModelByPK(Session sess, int iIntCustNum)
        {
            DataHelper helper = null;
            DataTable dtModel = null;
            try
            {
                helper = new DataHelper(sess);
                dtModel = helper.GetTable(CommandType.StoredProcedure, SelectSequel + " WHERE INTERNAL_VENDOR_NUM = @INTERNAL_VENDOR_NUM", new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_VENDOR_NUM", iIntCustNum)
                });
                if (!DataManager.IsEmpty(dtModel))
                {
                    return GetModel(dtModel.Rows[0]);
                }
                else
                {
                    return null;
                }
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }

        }

        #endregion

        #region "DataBase Operation"
        /// <summary>
        /// 在数据库里创建该数据访问对象的持久化数据，同时自增长属性INTERNAL_VENDOR_NUM也会同时得到更新,并提供数据库事务支持
        /// </summary>
        /// <remarks></remarks>
        public string Insert(Session sess)
        {
            DataHelper helper = null;
            IDataParameter[] paras = null;
            string stSQL = "CFG_IVendor";
            try
            {
                helper = new DataHelper(sess);
                paras = ValueParas(this);
                return "" + helper.GetInt(CommandType.StoredProcedure, stSQL, paras);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }

        }

        public string Delete(Session sess, VendorDO shpDtlDO)
        {
            DataHelper helper = null;
            string stSQL = "DELETE FROM VENDOR ";
            try
            {
                helper = new DataHelper(sess);
                helper.Update(CommandType.Text, stSQL + " WHERE INTERNAL_VENDOR_NUM = @INTERNAL_VENDOR_NUM", new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_VENDOR_NUM", this.INTERNAL_VENDOR_NUM)
                });
                return null;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return "MSG_GEN000";
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }

        }

        /// <summary>
        /// 将该数据访问对象的变化同步到数据库中，如果该数据访问对象在数据库中的持久数据不存在，同步不会发生，同时也不会抛出例外
        /// </summary>
        /// <remarks></remarks>
        public string Update(Session sess)
        {
            DataHelper helper = null;
            string stSQL = "CFG_UVendor";
            try
            {
                helper = new DataHelper(sess);
                helper.Update(CommandType.StoredProcedure, stSQL, ValueParas(this));
                return null;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return "MSG_GEN000";
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }

        }

        public void Update(Session sess, Dictionary<string, object> dcData)
        {
            DataHelper helper = null;
            StringBuilder sbSQL = null;
            string stSQL = null;
            int i = 0;
            IDataParameter[] paras = null;

            try
            {
                if (dcData.Count < 1)
                    return;
                helper = new DataHelper(sess);
                sbSQL = new StringBuilder(256)
                            .Append(" UPDATE VENDOR SET ");
                paras = new IDataParameter[dcData.Count + 1];
                foreach (KeyValuePair<string, object> kvp in dcData)
                {
                    sbSQL.Append(kvp.Key).Append(" = @Value").Append(i).Append(",");
                    paras[i] = new SqlParameter("@Value" + i, kvp.Value);
                    i++;
                }

                stSQL = sbSQL.ToString();
                stSQL = stSQL.Substring(0, stSQL.Length - 1);

                stSQL = stSQL + " WHERE INTERNAL_VENDOR_NUM = @INTERNAL_VENDOR_NUM";
                paras[i] = new SqlParameter("@INTERNAL_VENDOR_NUM", this.INTERNAL_VENDOR_NUM);

                helper.Update(CommandType.Text, stSQL, paras);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }
        #endregion

        #region "Data Load"
        /// <summary>
        /// 装载对象SHIPMENDETAIL的所有持久性对象到集合，如果数据量大，调用此方法会造成性能上的问题，请谨慎使用
        /// </summary>
        /// <remarks></remarks>
        public static List<VendorDO> GetAll(Session sess)
        {
            DataHelper helper = null;
            DataTable dtModel = null;
            string stSQL = SelectSequel;
            try
            {
                helper = new DataHelper(sess);
                dtModel = helper.GetTable(CommandType.Text, stSQL);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        /// <summary>
        /// 装载指定列的值等于指定值的的所有持久性SHIPMENDETAIL对象到集合
        /// </summary>
        /// <remarks></remarks>
        public static DataTable GetTableByColumn(Session sess, string columnName, Object value)
        {
            DataHelper helper = null;
            IDataParameter[] paras = null;
            string stSQL = null;

            try
            {
                helper = new DataHelper(sess);
                stSQL = SelectSequel + " Where [" + columnName + "] =@Value ";
                paras = new IDataParameter[]{
			new SqlParameter( "@Value", value)
		};
                return helper.GetTable(CommandType.Text, stSQL, paras);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public static List<VendorDO> GetListByColumn(Session sess, string columnName, Object value)
        {
            DataTable dtModel = null;

            try
            {
                dtModel = GetTableByColumn(sess, columnName, value);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
            }
        }


        /// <summary>
        /// 从DataView装载持久性SHIPMENDETAIL对象到集合
        /// </summary>
        /// <remarks></remarks>
        public static List<VendorDO> LoadListFromDataView(DataView dv)
        {
            List<VendorDO> list = new List<VendorDO>();
            for (int index = 0; index <= dv.Count - 1; index++)
            {
                list.Add(GetModel(dv[index].Row));
            }
            return list;
        }

        /// <summary>
        /// loading data value  to property from Datarow
        /// </summary>
        /// <remarks></remarks>
        private static VendorDO GetModel(DataRow row)
        {
            VendorDO obj = new VendorDO();
            if (!row["INTERNAL_VENDOR_NUM"].Equals(DBNull.Value)) obj.INTERNAL_VENDOR_NUM = Convert.ToInt32(row["INTERNAL_VENDOR_NUM"]);
            if (!row["VENDOR_ID"].Equals(DBNull.Value)) obj.VENDOR_ID = Convert.ToString(row["VENDOR_ID"]);
            if (!row["NAME"].Equals(DBNull.Value)) obj.NAME = Convert.ToString(row["NAME"]);
            if (!row["ADDRESS1"].Equals(DBNull.Value)) obj.ADDRESS1 = Convert.ToString(row["ADDRESS1"]);
            if (!row["ADDRESS2"].Equals(DBNull.Value)) obj.ADDRESS2 = Convert.ToString(row["ADDRESS2"]);
            if (!row["CITY"].Equals(DBNull.Value)) obj.CITY = Convert.ToString(row["CITY"]);
            if (!row["STATE"].Equals(DBNull.Value)) obj.STATE = Convert.ToString(row["STATE"]);
            if (!row["COUNTRY"].Equals(DBNull.Value)) obj.COUNTRY = Convert.ToString(row["COUNTRY"]);
            if (!row["POSTAL_CODE"].Equals(DBNull.Value)) obj.POSTAL_CODE = Convert.ToString(row["POSTAL_CODE"]);
            if (!row["PARENT"].Equals(DBNull.Value)) obj.PARENT = Convert.ToString(row["PARENT"]);
            if (!row["ATTENTION_TO"].Equals(DBNull.Value)) obj.ATTENTION_TO = Convert.ToString(row["ATTENTION_TO"]);
            if (!row["PHONE_NUM"].Equals(DBNull.Value)) obj.PHONE_NUM = Convert.ToString(row["PHONE_NUM"]);
            if (!row["FAX_NUM"].Equals(DBNull.Value)) obj.FAX_NUM = Convert.ToString(row["FAX_NUM"]);
            if (!row["EMAIL"].Equals(DBNull.Value)) obj.EMAIL = Convert.ToString(row["EMAIL"]);
            if (!row["ACTIVE"].Equals(DBNull.Value)) obj.ACTIVE = Convert.ToString(row["ACTIVE"]);
            if (!row["CARRIER"].Equals(DBNull.Value)) obj.CARRIER = Convert.ToString(row["CARRIER"]);
            if (!row["USER_STAMP"].Equals(DBNull.Value)) obj.USER_STAMP = Convert.ToString(row["USER_STAMP"]);
            if (!row["DATE_TIME_STAMP"].Equals(DBNull.Value)) obj.DATE_TIME_STAMP = Convert.ToDateTime(row["DATE_TIME_STAMP"]);

            return obj;
        }

        #endregion

        #region "Other function"
        /// <summary>
        /// 该数据访问对象从数据库中提取数据的Sql语句 
        /// </summary>
        /// <remarks></remarks>
        private static string SelectSequel
        {
            get
            {
                return "Select	*  From [VENDOR] ";
            }
        }

        /// <summary>
        /// 该数据访问对象的属性值装载到数据库更新参数数组
        /// </summary>
        /// <remarks></remarks>
        private static IDbDataParameter[] ValueParas(VendorDO obj)
        {
            SqlParameter[] paras = new SqlParameter[18];

            paras[0] = new SqlParameter("@INTERNAL_VENDOR_NUM", obj.INTERNAL_VENDOR_NUM);
            paras[0].DbType = DbType.String;
            paras[1] = new SqlParameter("@VENDOR_ID", obj.VENDOR_ID == String.Empty ? DBNull.Value : (object)obj.VENDOR_ID);
            paras[1].DbType = DbType.String;
            paras[2] = new SqlParameter("@NAME", obj.NAME == String.Empty ? DBNull.Value : (object)obj.NAME);
            paras[2].DbType = DbType.String;
            paras[3] = new SqlParameter("@ADDRESS1", obj.ADDRESS1 == String.Empty ? DBNull.Value : (object)obj.ADDRESS1);
            paras[3].DbType = DbType.String;
            paras[4] = new SqlParameter("@ADDRESS2", obj.ADDRESS2 == String.Empty ? DBNull.Value : (object)obj.ADDRESS2);
            paras[4].DbType = DbType.String;
            paras[5] = new SqlParameter("@CITY", obj.CITY == String.Empty ? DBNull.Value : (object)obj.CITY);
            paras[5].DbType = DbType.String;
            paras[6] = new SqlParameter("@STATE", obj.STATE == String.Empty ? DBNull.Value : (object)obj.STATE);
            paras[6].DbType = DbType.String;
            paras[7] = new SqlParameter("@COUNTRY", obj.COUNTRY == String.Empty ? DBNull.Value : (object)obj.COUNTRY);
            paras[7].DbType = DbType.String;
            paras[8] = new SqlParameter("@POSTAL_CODE", obj.POSTAL_CODE == String.Empty ? DBNull.Value : (object)obj.POSTAL_CODE);
            paras[8].DbType = DbType.String;
            paras[9] = new SqlParameter("@PARENT", obj.PARENT == String.Empty ? DBNull.Value : (object)obj.PARENT);
            paras[9].DbType = DbType.String;
            paras[10] = new SqlParameter("@ATTENTION_TO", obj.ATTENTION_TO == String.Empty ? DBNull.Value : (object)obj.ATTENTION_TO);
            paras[10].DbType = DbType.String;
            paras[11] = new SqlParameter("@PHONE_NUM", obj.PHONE_NUM == String.Empty ? DBNull.Value : (object)obj.PHONE_NUM);
            paras[11].DbType = DbType.String;
            paras[12] = new SqlParameter("@FAX_NUM", obj.FAX_NUM == String.Empty ? DBNull.Value : (object)obj.FAX_NUM);
            paras[12].DbType = DbType.String;
            paras[13] = new SqlParameter("@EMAIL", obj.EMAIL == String.Empty ? DBNull.Value : (object)obj.EMAIL);
            paras[13].DbType = DbType.String;
            paras[14] = new SqlParameter("@ACTIVE", obj.ACTIVE == String.Empty ? DBNull.Value : (object)obj.ACTIVE);
            paras[14].DbType = DbType.String;
            paras[15] = new SqlParameter("@CARRIER", obj.CARRIER == String.Empty ? DBNull.Value : (object)obj.CARRIER);
            paras[15].DbType = DbType.String;
            paras[16] = new SqlParameter("@USER_STAMP", obj.USER_STAMP == String.Empty ? DBNull.Value : (object)obj.USER_STAMP);
            paras[16].DbType = DbType.String;
            paras[17] = new SqlParameter("@DATE_TIME_STAMP", obj.DATE_TIME_STAMP);
            paras[17].DbType = DbType.DateTime;

            return paras;
        }

        private static IDbDataParameter[] UpdateWherePara(VendorDO shpDtlDO)
        {
            SqlParameter[] paras = new SqlParameter[1];
            paras[0] = new SqlParameter("@INTERNAL_VENDOR_NUM", shpDtlDO.INTERNAL_VENDOR_NUM);
            return paras;
        }

        #endregion


        public static List<VendorDO> GetListByColumns(Session sess, List<KeyValuePair<string, KeyValuePair<string, object>>> dcParam, string stOrderBy)
        {
            DataTable dtModel = null;

            try
            {
                dtModel = GetTableByColumns(sess, dcParam, stOrderBy);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
            }
        }

        public static DataTable GetTableByColumns(Session sess, List<KeyValuePair<string, KeyValuePair<string, object>>> dcParam, string stOrderBy)
        {
            DataHelper helper = null;
            StringBuilder sbSQL = null;
            int i = 0;
            IDataParameter[] pars = null;

            try
            {
                helper = new DataHelper(sess);
                sbSQL = new StringBuilder(SelectSequel)
                    .Append(" WHERE 1=1 ");

                pars = new IDataParameter[dcParam.Count];
                foreach (KeyValuePair<string, KeyValuePair<string, object>> de in dcParam)
                {
                    sbSQL.Append(" AND ").Append(de.Key).Append(de.Value.Key).Append("@Value").Append(i);
                    pars[i] = new SqlParameter("@Value" + i, de.Value.Value);
                    i++;
                }

                if (!StringManager.IsEmpty(stOrderBy))
                {
                    sbSQL.Append(stOrderBy);
                }
                return helper.GetTable(CommandType.Text, sbSQL.ToString(), pars);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }
    }
}
